package com.car.advertising.activity.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.car.advertising.R;
import com.car.advertising.activity.TaskDetailsActivity;
import com.car.advertising.activity.adapter.ProjectAdapter;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.entity.TaskRecordEntity;
import com.support.widget.JumpManager;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.layout.fragment.BaseRecyclerviewFreshFragment;
import com.support.widget.network.OkHttpClientManager;
import com.support.widget.util.LogUtil;
import com.support.widget.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目
 */
public class ProjectPageFragment extends BaseRecyclerviewFreshFragment {

    private List<TaskRecordEntity> projectList = new ArrayList<>();
    private ProjectAdapter projectAdapter;
    private int refreshNumber = 0;
    private boolean pullRefresh = false;

    @Override
    protected void init(Bundle savedInstanceState) {
        initTitleBackgourd(R.color.c_4D4D4D, R.color.color_white);
        initTitle("项目列表", "");

        getProjectListData(refreshNumber);
        setProjectList();
    }

    private void getProjectListData(int lastTaskId) {
        List<OkHttpClientManager.Param> params = new ArrayList<>();
//        params.add(new OkHttpClientManager.Param("type", ""));
        params.add(new OkHttpClientManager.Param("lastTaskId", "0"));
        params.add(new OkHttpClientManager.Param("limit", "20"));
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_037, new OkHttpClientManager.ResultCallback<List<TaskRecordEntity>>() {
            @Override
            public void onError(String request) {
                LogUtil.i(request);
            }

            @Override
            public void onResponse(List<TaskRecordEntity> resp) {
                if (!pullRefresh) {
                    projectList.clear();
                }
                if (resp == null || resp.size() == 0) {
                    ToastUtil.show(getContext(), "暂无新数据", Toast.LENGTH_SHORT);
                } else {
                    projectList.addAll(resp);
                    setProjectList();
                }
            }

            @Override
            public void onFinish() {
            }
        }, params, null);
    }

    private void setProjectList() {
        if (projectAdapter == null) {
            projectAdapter = new ProjectAdapter(getContext(), projectList);
            initRecyclerView(projectAdapter);
            projectAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(ViewGroup parent, View view, int position) {
                    Bundle bundle = new Bundle();
                    bundle.putString("taskId", projectList.get(position).getTaskId());
                    JumpManager.gotoActivity(getContext(), TaskDetailsActivity.class, false, bundle);
                }
            });
        } else {
            projectAdapter.refreshList(projectList);
        }
    }


    @Override
    protected void onRefreshFinish() {
        pullRefresh = false;
        refreshNumber = 0;
        stopLoad(false);
//        getProjectListData(refreshNumber);
    }

    @Override
    protected void onLoadMoreFinish() {
        pullRefresh = true;
        refreshNumber++;
        stopLoad(false);
//        getProjectListData(refreshNumber);
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onNoDoubleClick(View v) {

    }
}
